package com.example.code12.servlet;

import com.example.code12.bean.AdminBean;
import com.example.code12.dao.AdminDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 1.网址能够访问当前Java类
 * 2.能够接受网址传递过来的参数
 * 3.能将查询出来的数据传递给前端页面
 * 4.控制显示前端页面
 */
@WebServlet("/admin.jsp")
public class AdminServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        // todo获取分页页码参数
        String p = req.getParameter("page");
        int page = 1;
        if (p !=null && p.equals("")){//页面字符串不为空 强制转化为数字
            page = Integer.parseInt(p);
        }
        AdminDao adminDao = new AdminDao();
        int totalPage = adminDao.countPage();
        List<AdminBean> admin = adminDao.getList(page);
        req.setAttribute("admin", admin);
        req.setAttribute("currentPage", page);//当前页数
        req.setAttribute("totalPage", totalPage);//总页数
        //4 显示页面
        req.getRequestDispatcher("WEB-INF/admin.jsp").forward(req, resp);
    }
}